﻿using PetaPoco;
using System.Runtime.CompilerServices;

namespace BlazorAdmin.Extensions
{
    internal class SQLiteBatchProvider : BatchProvider
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="db"></param>
        public SQLiteBatchProvider(IDatabase db) : base(db) => BatchSize = 30;

        /// <summary>
        /// 生成插入语句方法
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="names"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        protected override string GenerateHeaderSql(string tableName, string[] names, string[] values)
        {
            DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new(23, 3);
            defaultInterpolatedStringHandler.AppendLiteral("INSERT INTO ");
            defaultInterpolatedStringHandler.AppendFormatted(tableName);
            defaultInterpolatedStringHandler.AppendLiteral(" (");
            defaultInterpolatedStringHandler.AppendFormatted(string.Join(",", names));
            defaultInterpolatedStringHandler.AppendLiteral(") select ");
            defaultInterpolatedStringHandler.AppendFormatted(string.Join(",", values));
            return defaultInterpolatedStringHandler.ToStringAndClear();
        }

        /// <summary>
        /// 生成选择语句方法
        /// </summary>
        /// <param name="values"></param>
        /// <returns></returns>
        protected override string GenerateBodySql(string[] values)
        {
            return " union all select " + string.Join(",", values);
        }
    }
}
